package com.sctvcloud.dongqu.ui.dialog;


import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jiongbull.jlog.JLog;
import com.ruihang.generalibrary.ui.dialog.BaseBottomSheetFrag;
import com.ruihang.generalibrary.utils.ListUtils;
import com.sctvcloud.dongqu.R;
import com.sctvcloud.dongqu.beans.GoodsBean;
import com.sctvcloud.dongqu.beans.ImageUr;
import com.sctvcloud.dongqu.ui.utils.GlideUtil;

/**
 * A simple {@link BaseBottomSheetFrag} subclass.
 * Use the {@link CommodityBuyFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class CommodityBuyFragment extends BaseBottomSheetFrag implements View.OnClickListener, View.OnLongClickListener {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private static final String ARG_PARAM3 = "param3";
    private static final String ARG_PARAM4 = "param4";

    protected ImageView ivPic;
    protected TextView tvPrice;
    protected TextView tvAddr;
    protected TextView tvNum;
    protected View vAdd;
    protected View vLess;
    protected View vBuy;


    private int num = 1;

    private float rmb;
    private int integral;
    private String addr;
    private String icon;


    private IBuyClick buyClick;


    public CommodityBuyFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param rmb 人民币价格
     * @return A new instance of fragment CommodityBuyFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static CommodityBuyFragment newInstance(float rmb, int integral, String icon, String addr) {
        CommodityBuyFragment fragment = new CommodityBuyFragment();
        Bundle args = new Bundle();
        args.putFloat(ARG_PARAM1, rmb);
        args.putInt(ARG_PARAM2, integral);
        args.putString(ARG_PARAM3, icon);
        args.putString(ARG_PARAM4, addr);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_commodity_buy;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            rmb = getArguments().getFloat(ARG_PARAM1);
            integral = getArguments().getInt(ARG_PARAM2);
            icon = getArguments().getString(ARG_PARAM3);
            addr = getArguments().getString(ARG_PARAM4);
            JLog.e("fffff  rmb=" + rmb + "  integral=" + integral + "  icon+" + icon + "  addr=" + addr);
        } else {
            JLog.e("fffff  arguments is null");
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        removeClick(vAdd, vBuy, vLess, tvAddr);
    }

    @Override
    public void initView() {
        ivPic = (ImageView) rootView.findViewById(R.id.commodity_buy_pic);
        tvAddr = (TextView) rootView.findViewById(R.id.commodity_buy_addre);
        tvNum = (TextView) rootView.findViewById(R.id.commodity_buy_num);
        tvPrice = (TextView) rootView.findViewById(R.id.commodity_buy_price);
        vAdd = rootView.findViewById(R.id.commodity_buy_add);
        vBuy = rootView.findViewById(R.id.commodity_buy_redeem);
        vLess = rootView.findViewById(R.id.commodity_buy_less);
        vAdd.setOnClickListener(this);
        vBuy.setOnClickListener(this);
        vLess.setOnClickListener(this);
        tvAddr.setOnClickListener(this);
        tvNum.setText(num + "");
        initData();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.commodity_buy_add) {
            modifyNum(true);
        } else if (id == R.id.commodity_buy_less) {
            modifyNum(false);
        } else if (id == R.id.commodity_buy_addre) {
            if (buyClick!=null){
                buyClick.onAddressClick();
            }
        }else if (id==R.id.commodity_buy_redeem){
            if (buyClick!=null){
                buyClick.onBuyClick(num);
            }
        }
    }

    @Override
    public boolean onLongClick(View v) {
        return false;
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        buyClick=null;
    }

    private void initData() {
        if (!TextUtils.isEmpty(icon)) {
            GlideUtil.loadImgWith11Def(getContext(), icon, ivPic);
        }
        if(rmb>0&&integral>0) {
            tvPrice.setText(getString(R.string.price_integral_f, rmb, integral));
        }else if(rmb>0){
            tvPrice.setText(getString(R.string.price_rmb, rmb));
        }else {
            tvPrice.setText(getString(R.string.price_integral, integral));
        }
        if (TextUtils.isEmpty(addr)) {
            tvAddr.setText(R.string.no_addrs);
        } else {
            tvAddr.setText(addr);
        }
    }

    public void setAddress(String addr) {
        this.addr = addr;
        Bundle args=getArguments();
        if (args!=null){
            args.putString(ARG_PARAM4, addr);
        }
        if (tvAddr != null) {
            if (TextUtils.isEmpty(addr)) {
                tvAddr.setText(R.string.no_addrs);
            } else {
                tvAddr.setText(addr);
            }
        }
    }

    private synchronized void modifyNum(boolean isAdd) {
        if (isAdd) {
            if (num < Integer.MAX_VALUE) {
                num++;
                tvNum.setText(num + "");
            }
        } else {
            if (num > 1) {
                num--;
                tvNum.setText(num + "");
            }
        }
    }

    public void setBuyClick(IBuyClick buyClick) {
        this.buyClick = buyClick;
    }

    public interface IBuyClick{
        void onBuyClick(int count);
        void onAddressClick();
    }

}
